import { deepClone } from "@/assets/utils/deepClone";

export const cPageEmit = ["getDetailHandle", "saveAfter", "id"];
export const cSearchEmit = ["getListFn", "showAdd"];
export const cDioEmit = ["getListFn"];
export class intialDataFn {
  obj = ref<ObjType>({});
  inialData = ref<PageDataType>({
    cDio: { formType: "cDio", formParams: {}, cformOpt: { items: {} }, cDioOpt: {} },
    cSearch: { formType: "cSearch", formParams: {}, cformOpt: { items: {} }, cSearchOpt: {} },
    cPage: { formType: "cPage", formParams: {}, cformOpt: { items: {} }, cPageOpt: {} },
    cTable: { cTableOpt: { list: { url: "" }, columns: [] } }
  });
  add(intialData: IntialDataType, refKey?: string) {
    let type = intialData.formType;
    this.obj.value = { ...this.obj.value, [refKey || type]: intialData };
    this.obj.value[refKey || type] = { ...this.inialData.value[type], ...this.obj.value[refKey || type], ["ref"]: (el: CSuperFormRefType) => (this.obj.value[refKey || type][refKey || type] = el) };
    return this.obj.value[refKey || type];
  }
  adds(intialDatas) {
    for (let item in intialDatas) {
      this.add(intialDatas[item], intialDatas[item.type], item);
    }
    return this.obj.value;
  }
}
export function intialFnFns(objed: { obj: ObjType; intialFn: intialFnType; cTableKey?: string; cDioKey?: string }, ref) {
  let { obj, intialFn } = objed;
  let allMethods = {
    showAdd({ row, other = { refKey: "cDio" } }) {
      let dio = obj.value[other.refKey];
      let dioData = row ? deepClone(row) : dio.formParams || {};
      dio[other.refKey || "cDio"]?.methods.showDialog(dioData);
    },
    async getListFn({ other, fn }) {
      let table = other.refKey;
      let cSearch = other.cSearchKey;
      let form = {};
      if (obj.value[cSearch]?.[cSearch]?.dataed?.cFormRef?.form) {
        let formObj = obj.value[cSearch]?.[cSearch]?.dataed?.cFormRef?.form;
        for (let item in formObj) {
          if (formObj[item]) form[item] = formObj[item];
        }
      }
      await obj.value[table][table]?.getListFn(form, other.isSearch);
      if (fn) fn();
    }
  };
  let methods = {};
  for (let item in intialFn) {
    methods[item] = intialFn[item].otherFn ? { ...intialFn[item].otherFn } : {};
    if (!intialFn[item].commonFn) return;
    intialFn[item].commonFn.map((itex) => {
      methods[item][itex.methodName] = (data) => {
        allMethods[itex.methodName]({ other: itex.other, ...data });
      };
    });
    let types = ["cDio", "cSearch", "cPage"];
    if (types.includes(item)) {
      methods[item].changeProps = (objd) => {
        let data = obj.value[item];
        objd.propsName.map((ite, index) => {
          if (index != objd.propsName.length - 1) {
            data = data[ite];
          }
        });
        data[objd.propsName[objd.propsName.length - 1]] = objd.e;
      };
    }
  }
  return methods;
}
export function intialFnFn(obj) {
  let methods = intialFnFns(obj);
  let methodsHandle = {};
  for (let item in methods) {
    methodsHandle = methods[item];
  }
  return methodsHandle;
}
export type CSuperFormPropsType = {
  formType: "cPage" | "cSearch" | "cDio";
  id?: string;
  cSearchOpt?: CSearchOptType;
  cDioOpt?: CDioOptType;
  cPageOpt?: CPageOptType;
  cformOpt?: CFormOptType;
  formParams?: { [key: string]: any };
};
